package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.InspectionDAO;
import cn.edu.scau.cmi.hombio.dao.PigHouseDAO;
import cn.edu.scau.cmi.hombio.dao.PigHouseTypeDAO;
import cn.edu.scau.cmi.hombio.dao.PigstyDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPighouseDAO;

import cn.edu.scau.cmi.hombio.domain.Inspection;
import cn.edu.scau.cmi.hombio.domain.PigHouse;
import cn.edu.scau.cmi.hombio.domain.PigHouseType;
import cn.edu.scau.cmi.hombio.domain.Pigsty;
import cn.edu.scau.cmi.hombio.domain.TaskPighouse;

import cn.edu.scau.cmi.hombio.service.PigHouseService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for PigHouse entities
 * 
 */

@Controller("PigHouseRestController")

public class PigHouseRestController {

	/**
	 * DAO injected by Spring that manages Inspection entities
	 * 
	 */
	@Autowired
	private InspectionDAO inspectionDAO;

	/**
	 * DAO injected by Spring that manages PigHouse entities
	 * 
	 */
	@Autowired
	private PigHouseDAO pigHouseDAO;

	/**
	 * DAO injected by Spring that manages PigHouseType entities
	 * 
	 */
	@Autowired
	private PigHouseTypeDAO pigHouseTypeDAO;

	/**
	 * DAO injected by Spring that manages Pigsty entities
	 * 
	 */
	@Autowired
	private PigstyDAO pigstyDAO;

	/**
	 * DAO injected by Spring that manages TaskPighouse entities
	 * 
	 */
	@Autowired
	private TaskPighouseDAO taskPighouseDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for PigHouse entities
	 * 
	 */
	@Autowired
	private PigHouseService pigHouseService;

	/**
	 * Create a new TaskPighouse entity
	 * 
	 */
	@RequestMapping(value = "/PigHouse/{pighouse_id}/taskPighouses", method = RequestMethod.POST)
	@ResponseBody
	public TaskPighouse newPigHouseTaskPighouses(@PathVariable Integer pighouse_id, @RequestBody TaskPighouse taskpighouse) {
		pigHouseService.savePigHouseTaskPighouses(pighouse_id, taskpighouse);
		return taskPighouseDAO.findTaskPighouseByPrimaryKey(taskpighouse.getId());
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Save an existing PigHouseType entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/pigHouseType", method = RequestMethod.PUT)
	@ResponseBody
	public PigHouseType savePigHousePigHouseType(@PathVariable Integer pighouse_id, @RequestBody PigHouseType pighousetype) {
		pigHouseService.savePigHousePigHouseType(pighouse_id, pighousetype);
		return pigHouseTypeDAO.findPigHouseTypeByPrimaryKey(pighousetype.getId());
	}

	/**
	* Save an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/pigsties", method = RequestMethod.PUT)
	@ResponseBody
	public Pigsty savePigHousePigsties(@PathVariable Integer pighouse_id, @RequestBody Pigsty pigsties) {
		pigHouseService.savePigHousePigsties(pighouse_id, pigsties);
		return pigstyDAO.findPigstyByPrimaryKey(pigsties.getId());
	}

	/**
	* Create a new PigHouse entity
	* 
	*/
	@RequestMapping(value = "/PigHouse", method = RequestMethod.POST)
	@ResponseBody
	public PigHouse newPigHouse(@RequestBody PigHouse pighouse) {
		pigHouseService.savePigHouse(pighouse);
		return pigHouseDAO.findPigHouseByPrimaryKey(pighouse.getId());
	}

	/**
	* View an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/pigsties/{pigsty_id}", method = RequestMethod.GET)
	@ResponseBody
	public Pigsty loadPigHousePigsties(@PathVariable Integer pighouse_id, @PathVariable Integer related_pigsties_id) {
		Pigsty pigsty = pigstyDAO.findPigstyByPrimaryKey(related_pigsties_id, -1, -1);

		return pigsty;
	}

	/**
	* Get PigHouseType entity by PigHouse
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/pigHouseType", method = RequestMethod.GET)
	@ResponseBody
	public PigHouseType getPigHousePigHouseType(@PathVariable Integer pighouse_id) {
		return pigHouseDAO.findPigHouseByPrimaryKey(pighouse_id).getPigHouseType();
	}

	/**
	* Create a new Pigsty entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/pigsties", method = RequestMethod.POST)
	@ResponseBody
	public Pigsty newPigHousePigsties(@PathVariable Integer pighouse_id, @RequestBody Pigsty pigsty) {
		pigHouseService.savePigHousePigsties(pighouse_id, pigsty);
		return pigstyDAO.findPigstyByPrimaryKey(pigsty.getId());
	}

	/**
	* Save an existing PigHouse entity
	* 
	*/
	@RequestMapping(value = "/PigHouse", method = RequestMethod.PUT)
	@ResponseBody
	public PigHouse savePigHouse(@RequestBody PigHouse pighouse) {
		pigHouseService.savePigHouse(pighouse);
		return pigHouseDAO.findPigHouseByPrimaryKey(pighouse.getId());
	}

	/**
	* Save an existing TaskPighouse entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/taskPighouses", method = RequestMethod.PUT)
	@ResponseBody
	public TaskPighouse savePigHouseTaskPighouses(@PathVariable Integer pighouse_id, @RequestBody TaskPighouse taskpighouses) {
		pigHouseService.savePigHouseTaskPighouses(pighouse_id, taskpighouses);
		return taskPighouseDAO.findTaskPighouseByPrimaryKey(taskpighouses.getId());
	}

	/**
	* Select an existing PigHouse entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}", method = RequestMethod.GET)
	@ResponseBody
	public PigHouse loadPigHouse(@PathVariable Integer pighouse_id) {
		return pigHouseDAO.findPigHouseByPrimaryKey(pighouse_id);
	}

	/**
	* Create a new Inspection entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/inspections", method = RequestMethod.POST)
	@ResponseBody
	public Inspection newPigHouseInspections(@PathVariable Integer pighouse_id, @RequestBody Inspection inspection) {
		pigHouseService.savePigHouseInspections(pighouse_id, inspection);
		return inspectionDAO.findInspectionByPrimaryKey(inspection.getId());
	}

	/**
	* Delete an existing PigHouse entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigHouse(@PathVariable Integer pighouse_id) {
		PigHouse pighouse = pigHouseDAO.findPigHouseByPrimaryKey(pighouse_id);
		pigHouseService.deletePigHouse(pighouse);
	}

	/**
	* Create a new PigHouseType entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/pigHouseType", method = RequestMethod.POST)
	@ResponseBody
	public PigHouseType newPigHousePigHouseType(@PathVariable Integer pighouse_id, @RequestBody PigHouseType pighousetype) {
		pigHouseService.savePigHousePigHouseType(pighouse_id, pighousetype);
		return pigHouseTypeDAO.findPigHouseTypeByPrimaryKey(pighousetype.getId());
	}

	/**
	* Delete an existing Pigsty entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/pigsties/{pigsty_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigHousePigsties(@PathVariable Integer pighouse_id, @PathVariable Integer related_pigsties_id) {
		pigHouseService.deletePigHousePigsties(pighouse_id, related_pigsties_id);
	}

	/**
	* Delete an existing TaskPighouse entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/taskPighouses/{taskpighouse_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigHouseTaskPighouses(@PathVariable Integer pighouse_id, @PathVariable Integer related_taskpighouses_id) {
		pigHouseService.deletePigHouseTaskPighouses(pighouse_id, related_taskpighouses_id);
	}

	/**
	* View an existing TaskPighouse entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/taskPighouses/{taskpighouse_id}", method = RequestMethod.GET)
	@ResponseBody
	public TaskPighouse loadPigHouseTaskPighouses(@PathVariable Integer pighouse_id, @PathVariable Integer related_taskpighouses_id) {
		TaskPighouse taskpighouse = taskPighouseDAO.findTaskPighouseByPrimaryKey(related_taskpighouses_id, -1, -1);

		return taskpighouse;
	}

	/**
	* Save an existing Inspection entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/inspections", method = RequestMethod.PUT)
	@ResponseBody
	public Inspection savePigHouseInspections(@PathVariable Integer pighouse_id, @RequestBody Inspection inspections) {
		pigHouseService.savePigHouseInspections(pighouse_id, inspections);
		return inspectionDAO.findInspectionByPrimaryKey(inspections.getId());
	}

	/**
	* View an existing Inspection entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/inspections/{inspection_id}", method = RequestMethod.GET)
	@ResponseBody
	public Inspection loadPigHouseInspections(@PathVariable Integer pighouse_id, @PathVariable Integer related_inspections_id) {
		Inspection inspection = inspectionDAO.findInspectionByPrimaryKey(related_inspections_id, -1, -1);

		return inspection;
	}

	/**
	* View an existing PigHouseType entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/pigHouseType/{pighousetype_id}", method = RequestMethod.GET)
	@ResponseBody
	public PigHouseType loadPigHousePigHouseType(@PathVariable Integer pighouse_id, @PathVariable Integer related_pighousetype_id) {
		PigHouseType pighousetype = pigHouseTypeDAO.findPigHouseTypeByPrimaryKey(related_pighousetype_id, -1, -1);

		return pighousetype;
	}

	/**
	* Delete an existing Inspection entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/inspections/{inspection_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigHouseInspections(@PathVariable Integer pighouse_id, @PathVariable Integer related_inspections_id) {
		pigHouseService.deletePigHouseInspections(pighouse_id, related_inspections_id);
	}

	/**
	* Show all Inspection entities by PigHouse
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/inspections", method = RequestMethod.GET)
	@ResponseBody
	public List<Inspection> getPigHouseInspections(@PathVariable Integer pighouse_id) {
		return new java.util.ArrayList<Inspection>(pigHouseDAO.findPigHouseByPrimaryKey(pighouse_id).getInspections());
	}

	/**
	* Show all PigHouse entities
	* 
	*/
	@RequestMapping(value = "/PigHouse", method = RequestMethod.GET)
	@ResponseBody
	public List<PigHouse> listPigHouses() {
		return new java.util.ArrayList<PigHouse>(pigHouseService.loadPigHouses());
	}

	/**
	* Delete an existing PigHouseType entity
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/pigHouseType/{pighousetype_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deletePigHousePigHouseType(@PathVariable Integer pighouse_id, @PathVariable Integer related_pighousetype_id) {
		pigHouseService.deletePigHousePigHouseType(pighouse_id, related_pighousetype_id);
	}

	/**
	* Show all Pigsty entities by PigHouse
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/pigsties", method = RequestMethod.GET)
	@ResponseBody
	public List<Pigsty> getPigHousePigsties(@PathVariable Integer pighouse_id) {
		return new java.util.ArrayList<Pigsty>(pigHouseDAO.findPigHouseByPrimaryKey(pighouse_id).getPigsties());
	}

	/**
	* Show all TaskPighouse entities by PigHouse
	* 
	*/
	@RequestMapping(value = "/PigHouse/{pighouse_id}/taskPighouses", method = RequestMethod.GET)
	@ResponseBody
	public List<TaskPighouse> getPigHouseTaskPighouses(@PathVariable Integer pighouse_id) {
		return new java.util.ArrayList<TaskPighouse>(pigHouseDAO.findPigHouseByPrimaryKey(pighouse_id).getTaskPighouses());
	}
}